/*
 * @Author: zhangweixin 
 * @Date: 2018-12-04 19:37:49 
 * @Last Modified by: zhangweixin
 * @Last Modified time: 2018-12-14 18:40:52
 */

 export function getParams(search){
  let str = search || location.search;
  let result = {};
  str.replace(/(\?|&)([^=&]+)=([^&]+)/g, function(g1, g2, key, value) {
    // console.log(g1)
    // console.log(g2)
    // console.log(key)
    // console.log(value)
    result[key] = value;
  });
  return result;
 }

//  var c = {a: {b : [1,2,3] }}
// get(c ,'a.b')     // [1,2,3]
// get(c, 'a.b[1]')  // 2
// get(c, 'a.d', 12)  // 12
 export function getObjKeyValue (obj, props, def) {
  if((obj == null) || obj == null || typeof props !== 'string') return def;
  const temp = props.split('.');
  const fieldArr = [].concat(temp);
  temp.forEach((e, i) => {
      if(/^(\w+)\[(\w+)\]$/.test(e)) {
          const matchs = e.match(/^(\w+)\[(\w+)\]$/);
          const field1 = matchs[1];
          const field2 = matchs[2];
          const index = fieldArr.indexOf(e);
          fieldArr.splice(index, 1, field1, field2);
      }
  })
  return fieldArr.reduce((pre, cur) => {
      const target = pre[cur] || def;

      if(target instanceof Array) {
          return [].concat(target);
      }
      if(target instanceof Object) {
          return Object.assign({}, target)
      }
      return target;
  }, obj)
}